/*************************************************************************
*
*	sendretr.c
*
*  Copyright 2007,2008 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions which send or retrieve files from
*    the dataserver.
*
**************************************************************************
*
*  version 1.1.4, 10/4/08, SBS
*    - documentation
*
*  version 1.1.3, 3/21/08, SBS
*    - minor cleanup
*
*  version 1.1.2, 3/9/08, SBS
*    - add FetchFileS
*
*  version 1.1.1, 1/28/08, SBS
*    - "static char" -> "char"
*    - minor cleanup
*
*  version 1.1, 10/3/07, SBS
*    - moved chmod_key into here
*
*  version 1.0, 7/3/07, SBS
*    - split from archiver.c
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup common
*  @brief
*  This module provides functions which send or retrieve files from
*    the dataserver.
*  <hr>
**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "logfile.h"

#include "sendretr.h"

#define CMDSTRLENGTH 512
#define FILENAMELENGTH 128


/************************************************
*
* sendretr_ChmodKey()
*
* Changes permissions on the ssh identity key
*    because we have had problems with permissions
*    in the past
*
* Parameters:
*   cf   - configuration data
*
* Returns:
*   none
*
************************************************/
void sendretr_ChmodKey(CONFIGDATA cf)
{
	char cmd[CMDSTRLENGTH];
	
	snprintf(cmd,CMDSTRLENGTH,"chmod 600 %s", cf.identitykey);
	
	system(cmd);
	
	return;
}


/************************************************
*
* sendretr_FetchFile()
*
* Retrieves a file from the dataserver.
*
* Parameters:
*   cf       - configuration data
*   robnum   - robot number
*   filebase - filename base ("command" or "telemetry")
*
* Returns: 
*    none
*
************************************************/
int sendretr_FetchFile(CONFIGDATA cf, int robnum, char *filebase)
{
/*
	static char cmd[CMDSTRLENGTH];
	static char filespec[FILENAMELENGTH];
	static char url[FILENAMELENGTH];
*/
	char cmd[CMDSTRLENGTH];
	char filespec[FILENAMELENGTH];
	char url[FILENAMELENGTH];

	int rval;

#ifdef DEBUG
	char temp[LOGSTRLEN];
#endif

	snprintf(filespec,FILENAMELENGTH
				,"%s%d.%s"
				,filebase
				,robnum+1
				,"txt");

	snprintf(url,FILENAMELENGTH
				,"%s/%s"
				,cf.remotepath
				,filespec);
				
#ifdef DEBUG
	snprintf(temp,LOGSTRLEN,"url = %s",url);				
	logfile_LogPrint(temp);
#endif

	snprintf(cmd,CMDSTRLENGTH
				,"%s %s@%s:%s ."
				,cf.retrcommand
				,cf.username
				,cf.servername
//				,"taosf/gsfc/telemetry1.txt .");
				,url);

#ifdef DEBUG
	snprintf(temp, LOGSTRLEN, "-----command is %s", cmd);
	logfile_LogPrint(temp);
#endif

	rval = system(cmd);
	
#ifdef DEBUG
	snprintf(temp, LOGSTRLEN, "rsync returned %d\n",rval);
	logfile_LogPrint(temp);
#endif			
	
	return(rval);
}
	

/************************************************
*
* sendretr_FetchFileS()
*
* Grabs multiple files (all robots) from the dataserver.
*
* Parameters:
*   cf       - configuration data
*   filebase - filename base ("command" or "telemetry")
*
* Returns: 
*    none
*
************************************************/
int sendretr_FetchFileS(CONFIGDATA cf, char *filebase)
{
	char cmd[CMDSTRLENGTH];
	char filespec[FILENAMELENGTH];
	char url[FILENAMELENGTH];

	int rval;

	#ifdef DEBUG
	char temp[LOGSTRLEN];
	#endif

	snprintf(filespec,FILENAMELENGTH
				,"%s*.%s"
				,filebase
				,"txt");

	snprintf(url,FILENAMELENGTH
				,"%s/%s"
				,cf.remotepath
				,filespec);
				
	#ifdef DEBUG
	snprintf(temp,LOGSTRLEN,"url = %s",url);				
	logfile_LogPrint(temp);
	#endif

	snprintf(cmd,CMDSTRLENGTH
				,"%s %s@%s:%s ."
				,cf.retrcommand
				,cf.username
				,cf.servername
				,url);

	#ifdef DEBUG
	snprintf(temp, LOGSTRLEN, "-----command is %s", cmd);
	logfile_LogPrint(temp);
	#endif

	rval = system(cmd);
	
	#ifdef DEBUG
	snprintf(temp, LOGSTRLEN, "rsync returned %d\n",rval);
	logfile_LogPrint(temp);
	#endif			
	
	return(rval);
}
	

/************************************************
*
* sendretr_PutFile()
*
* Sends a file to the dataserver
*
* Parameters:
*   cf       - configuration data
*   robnum   - which robot
*   filebase - filename base ("command" or "telemetry")
*
* Returns:
*   none
*
************************************************/
void sendretr_PutFile(CONFIGDATA cf, int robnum, char *filebase)
{
	char cmd[CMDSTRLENGTH];

	#ifdef DEBUG
	char temp[LOGSTRLEN];
	#endif

	snprintf(cmd
			,CMDSTRLENGTH
			,"%s %s %s%d.%s %s@%s:%s"
			,cf.sendcommand
			,cf.identitykey
			,filebase
			,robnum
			,cf.telemfileext
			,cf.username
			,cf.servername
			,cf.remotepath);
			
	#ifdef DEBUG
	snprintf(temp,LOGSTRLEN,"=====SBS===== Command is %s\n",cmd);
	logfile_LogPrint(temp);
	#endif

	system(cmd);

	return;
}
